package com.qiaofang.collectmanage.stub.feign.client.impl;
import java.util.List;

import com.qiaofang.collectmanage.stub.request.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qiaofang.common.constant.CommonResponseCode;
import com.qiaofang.common.exception.BusinessException;
import com.qiaofang.collectmanage.stub.bean.CrawlerSubTaskDTO;
import com.qiaofang.collectmanage.stub.bean.SaveCrawlerSubTaskDTO;
import com.qiaofang.collectmanage.stub.bean.UpdateCrawlerSubTaskDTO;
import com.qiaofang.common.model.page.PageDTO;
import com.qiaofang.collectmanage.stub.bean.SearchCrawlerSubTaskDTO;
import com.qiaofang.collectmanage.stub.bean.SearchSubTaskResultDTO;
import com.qiaofang.collectmanage.stub.response.SearchSubTaskResponse;
import com.qiaofang.collectmanage.stub.facade.CrawlerSubTaskFacade;
import com.qiaofang.collectmanage.stub.feign.client.CrawlerSubTaskClient;
import com.qiaofang.collectmanage.stub.response.ListSubTaskResponse;
import com.qiaofang.collectmanage.stub.response.RemoveSubTaskResponse;
import com.qiaofang.collectmanage.stub.response.SaveSubTaskResponse;
import com.qiaofang.collectmanage.stub.response.UpdateSubTaskResponse;
import com.qiaofang.collectmanage.stub.response.GetSubTaskBySubTaskResponse;
import com.qiaofang.collectmanage.stub.bean.ListCrawlerSubTaskDTO;

import com.qiaofang.collectmanage.stub.response.GetSubTaskByUuidResponse;
import com.qiaofang.collectmanage.stub.response.ListSubTaskByUuidsResponse;
import com.qiaofang.collectmanage.stub.request.RemoveCrawlerSubTaskRequest;
/**
* Created by generator on 2018-8-1 14:39:02
*/
@Service("subTaskClient")
public class CrawlerSubTaskClientImpl implements CrawlerSubTaskClient {

    @Autowired
    private CrawlerSubTaskFacade subTaskFacade;

    @Override
    public CrawlerSubTaskDTO getSubTaskByUuid(String uuid){
        GetCrawlerSubTaskByUuidRequest request = new GetCrawlerSubTaskByUuidRequest();
        request.setUuid(uuid);
        GetSubTaskByUuidResponse response = subTaskFacade.getCrawlerSubTaskByUuid(request);
        if (CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
             return response.getSubTaskDTO();
        } else {
            throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
        }
    }
  @Override
   public CrawlerSubTaskDTO getSubTaskBySubTask(CrawlerSubTaskDTO dto){
        GetCrawlerSubTaskBySubTaskRequest request = new GetCrawlerSubTaskBySubTaskRequest();
        request.setSubTaskDTO(dto);
        GetSubTaskBySubTaskResponse response = subTaskFacade.getCrawlerSubTaskBySubTask(request);
        if (CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
            return response.getSubTaskDTO();
        } else {
           throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
        }
   }

   @Override
    public void saveSubTask(SaveCrawlerSubTaskDTO dto) {
        SaveCrawlerSubTaskRequest request = new SaveCrawlerSubTaskRequest();
        request.setSubTaskDTO(dto);
        SaveSubTaskResponse  response = subTaskFacade.saveCrawlerSubTask(request);
        if (!CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
            throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
        }
    }

    @Override
    public void updateSubTask (UpdateCrawlerSubTaskDTO dto) {
        UpdateCrawlerSubTaskRequest request = new UpdateCrawlerSubTaskRequest();
        request.setSubTaskDTO(dto);
        UpdateSubTaskResponse  response = subTaskFacade.updateCrawlerSubTask(request);
        if (!CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
           throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
        }
    }
    @Override
    public void removeSubTaskByUuid (String uuid) {
        RemoveCrawlerSubTaskRequest request = new RemoveCrawlerSubTaskRequest();
        request.setUuid(uuid);
        RemoveSubTaskResponse  response = subTaskFacade.removeCrawlerSubTask(request);
        if (!CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
           throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
        }
    }
    @Override
    public List<CrawlerSubTaskDTO> listSubTask(ListCrawlerSubTaskDTO dto){
        ListCrawlerSubTaskRequest request = new ListCrawlerSubTaskRequest();
        request.setSubTaskDTO(dto);
        ListSubTaskResponse  response = subTaskFacade.listCrawlerSubTask(request);
        if (CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
           return response.getList();
        } else {
           throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
        }
    }
        @Override
        public List<CrawlerSubTaskDTO> listSubTaskByUuids(List<String> uuidList){
            ListCrawlerSubTaskByUuidsRequest request = new ListCrawlerSubTaskByUuidsRequest();
            request.setSubTaskUuidList(uuidList);
            ListSubTaskByUuidsResponse  response = subTaskFacade.listCrawlerSubTaskByUuids(request);
            if (CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
            return response.getList();
            } else {
            throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
            }
       }
    @Override
    public SearchSubTaskResultDTO searchSubTask(PageDTO pageDTO,SearchCrawlerSubTaskDTO dto){
        SearchCrawlerSubTaskRequest request = new SearchCrawlerSubTaskRequest();
        request.setSearchSubTaskDTO(dto);
        request.setPageDTO(pageDTO);
        SearchSubTaskResponse  response = subTaskFacade.searchCrawlerSubTask(request);
        if (CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
               SearchSubTaskResultDTO result = new SearchSubTaskResultDTO();
               result.setList(response.getList());
               result.setCount(response.getCount());
               return result;
        } else {
            throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
        }
     }


}
